package com.kgc.springboot.aviation.controller.zzy;

import com.github.pagehelper.PageInfo;
import com.kgc.springboot.aviation.pojo.*;


import com.kgc.springboot.aviation.service.jianghongkun.LawsService;
import com.kgc.springboot.aviation.service.lvwenjia.LawsServices;
import com.kgc.springboot.aviation.service.zzy.AllService;
import com.kgc.springboot.aviation.service.zzy.BranchService;
import com.kgc.springboot.aviation.service.zzy.MenuServices;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


@Controller
public class Allcontroller {

    @Resource
    AllService allService;
    @Resource
    LawsServices lawsServices;

    @Resource
    MenuServices menuServices;
    @Resource
    BranchService branchService;


    @RequestMapping("/text1")
    public String bbbb(Model model,
                       @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                       @RequestParam(value = "lawsId", required = false, defaultValue = "0") Integer lawsId) {

        System.out.println(lawsId);
        Integer pageSize = 10;
        PageInfo<Directory> pageInfo = allService.selectAll(pageNum, pageSize, lawsId);
        List<Laws> lawss = lawsServices.getLawsList("");
        model.addAttribute("laws1", lawss);
        model.addAttribute("pageInfo", pageInfo);

        return "directory";
    }

    @RequestMapping("/users")
    public String userManager(Model model, HttpServletResponse response, @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum, @RequestParam(value = "userUname", required = false, defaultValue = "") String userUname, @RequestParam(value = "userPhone", required = false, defaultValue = "") String userPhone) {
        Integer pageSize = 100;
        PageInfo<User> pageInfo = allService.selectAllUser(pageNum, pageSize, userUname, userPhone);
        model.addAttribute(pageInfo);
        return "用户管理";
    }

    @RequestMapping("/text5")
    @ResponseBody
    public String test05(Integer id, Model model, HttpServletResponse response, Integer lawsId, String branchName, Integer branchSorting) {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = null;

        System.out.println("32131");

        Branch branch = new Branch();
        branch.setBranchName(branchName);
        branch.setBranchSorting(branchSorting);
        branch.setLawsId(lawsId);
        branch.setMenuId(id);
        int i = branchService.add(branch);
        if (i > 0) {

            return "<script>alert('添加成功！');location.href='/text1'</script>";

        } else {
            return "<script>alert('添加失败！');history.back();</script>";
        }

    }

    @RequestMapping("/test5/{id}")
    public String test50(@PathVariable("id") int id, Model model) {
        System.out.println("1233");
        model.addAttribute("id", id);
        List<Laws> lawss = lawsServices.getLawsList("");
        model.addAttribute("laws2", lawss);

        return "adddirectory";
    }

    @RequestMapping("/text6")
    @ResponseBody
    public String test06(Integer id, Model model, String menuName, Integer menuSorting, Integer lawsId, HttpServletResponse response) {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = null;

        System.out.println("11111111");
        Menu menu = new Menu();
        menu.setMenuName(menuName);
        menu.setMenuSorting(menuSorting);
        menu.setLawsId(lawsId);
        menu.setBranchId(id);
        int i = menuServices.add(menu);
        if (i > 0) {
            return "<script>alert('添加成功！');location.href='/text1'</script>";

        } else {
            return "<script>alert('添加失败！');history.back();</script>";
        }
    }

    @RequestMapping("/test6/{id}")
    public String test60(@PathVariable("id") int id, Model model) {
        System.out.println("12313");
        model.addAttribute("id", id);
        List<Laws> lawss = lawsServices.getLawsList("");
        model.addAttribute("laws3", lawss);

        return "adddirectory2";
    }

    @RequestMapping("/test7/{id}")
    public String test7(@PathVariable("id") Integer id, Model model) {
        Directory directory = allService.upGetAll(id);
        List<Laws> lawss = lawsServices.getLawsList("");
        model.addAttribute("laws4", lawss);
        model.addAttribute("id", id);
        model.addAttribute("directory", directory);
        return "updirectory";
    }

    @RequestMapping("/text7")
    @ResponseBody
    public String test07(Integer id, Model model, HttpServletResponse response, Integer lawsId, String directoryName, Integer directorySorting) {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = null;

        System.out.println("32131");

        Directory directory = new Directory();
        directory.setId(id);
        directory.setDirectoryName(directoryName);
        directory.setDirectorySorting(directorySorting);
        directory.setLawsId(lawsId);

        System.out.println(directoryName);
        System.out.println(directorySorting);
        System.out.println(lawsId);
        int i = allService.upDate(directory);
        if (i > 0) {

            return "<script>alert('修改成功！');location.href='/text1'</script>";

        } else {
            return "<script>alert('修改失败！');history.back();</script>";
        }

    }

    @RequestMapping("/test8")
    public String test8(Integer id,Integer id2, Model model) {
        Branch branch = branchService.upGetAll(id);
        List<Laws> lawss = lawsServices.getLawsList("");
        model.addAttribute("laws5", lawss);
        model.addAttribute("id", id);
        model.addAttribute("id2", id2);
        model.addAttribute("branch", branch);
        return "updirectory1";
    }

    @RequestMapping("/text8")
    @ResponseBody
    public String test08(Integer id,Integer id2, Model model, HttpServletResponse response, Integer lawsId, String branchName, Integer branchSorting) {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = null;

        Branch branch = new Branch();
        branch.setId(id);
        branch.setBranchName(branchName);
        branch.setBranchSorting(branchSorting);
        branch.setLawsId(lawsId);
        branch.setMenuId(id2);
        int i = branchService.upDate(branch);
        if (i > 0) {
            return "<script>alert('修改成功！');location.href='/text1'</script>";

        } else {
            return "<script>alert('修改失败！');history.back();</script>";
        }

    }

    @RequestMapping("/test9")
    public String test9( Integer id,Integer id3, Model model) {
        Menu menu = menuServices.getAll(id);
        List<Laws> lawss = lawsServices.getLawsList("");
        model.addAttribute("laws6", lawss);
        model.addAttribute("id", id);
        model.addAttribute("id3", id3);
        model.addAttribute("menu", menu);
        return "updirectory2";
    }

    @RequestMapping("/text9")
    @ResponseBody
    public String test09(Integer id,Integer id3, Model model, String menuName, Integer menuSorting, Integer lawsId, HttpServletResponse response) {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = null;


        Menu menu = new Menu();
        menu.setId(id);
        menu.setMenuName(menuName);
        menu.setMenuSorting(menuSorting);
        menu.setLawsId(lawsId);
        menu.setBranchId(id3);
        int i = menuServices.upDate(menu);
        if (i > 0) {
            return "<script>alert('修改成功！');location.href='/text1'</script>";

        } else {
            return "<script>alert('修改失败！');history.back();</script>";
        }
    }

    //收藏方法
    @RequestMapping("/addConllection")
    public void addConllection(Integer currentId, Integer clauseId, Integer lawsId, HttpSession session, Model model, HttpServletResponse response) throws IOException {
        PrintWriter out = response.getWriter();
        Collection collection = new Collection();
        Users users = (Users) session.getAttribute("users");

        collection.setUserId(users.getId());
        collection.setCurrentId(currentId);
        collection.setClauseId(clauseId);
        collection.setLawsId(lawsId);
        int i = allService.addCollection(collection);
        boolean flag = false;
        if (i > 0) {
            flag = true;
        }
        out.print(flag);
        out.flush();
        out.close();
    }

    //再次取消收藏方法
    @RequestMapping("/deleteConllection")
    public void deleteConllection(Integer currentId, Integer clauseId, Integer lawsId, HttpSession session, Model model, HttpServletResponse response) throws IOException {

        PrintWriter out = response.getWriter();
        Users users = (Users) session.getAttribute("users");
        int i = allService.deleteConllection(users.getId(), clauseId);
        boolean flag = false;
        if (i > 0) {
            flag = true;
        }
        out.print(flag);
        out.flush();
        out.close();
    }


    @RequestMapping("/selectCollection")
    public String selectCollection(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum, HttpSession session, Model model,HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        Integer pageSize = 10;
        PrintWriter out = response.getWriter();
        Users users = (Users) session.getAttribute("users");
        if (users != null) {
            PageInfo<Collection> pageInfo = allService.selectCollection(pageNum, pageSize, users.getId());
            model.addAttribute("pageInfo", pageInfo);
            return "我的收藏";
        } else {
            return "index3";
        }




    }

    @RequestMapping("/delConllection")
    public void delConllection(Integer id, HttpServletResponse response, HttpSession session) throws IOException {
        PrintWriter out = response.getWriter();

        Users users = (Users) session.getAttribute("users");
        int i = allService.delConllection(users.getId(),id);

        boolean flag = false;
        if (i > 0) {
            flag = true;
        }
        out.print(flag);
        out.flush();
        out.close();
    }

}
